<?php
	namespace Util;
	use Util\Httpclient;
	/**
	* 微信服务号权限验证获取
	*/
	class WechatTicket{
		private $APPID ;
		private $SECRET ;
		private $request;

		public function __construct(){
			$this->request = new Httpclient();
		} 

		public function set($appId,$secret){
			$this->APPID = $appId;
			$this->SECRET = $secret;
		}

		//用于微信登录CODE的获取
		public function getCode($redirect_uri,$sign=true){
			$APPID = $this->APPID;
			$uri = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$APPID&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo";
			if($sign){
				$uri .= "&scope=snsapi_base";
			}
			redirect($uri);
		}

		//用于微信用户登录后信息的获取
		public function WechatLogin(){
			$APPID = $this->APPID;
			$APPSECRET = $this->SECRET;
			$CODE = $_GET['code'];

			if(isset($CODE) && !empty($CODE)){
				$open_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$APPID&secret=$APPSECRET&code=$CODE&grant_type=authorization_code";

				$out = $this->request->quickGet($open_url);
				$token=json_decode($out,true);

				$access_token = $token['access_token'];
				$openid = $token['openid'];

				$userinfo_url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
				$userinfo = $this->request->quickGet($userinfo_url);

				$userinfo = json_decode($userinfo,true);
			}else{
				$userinfo = "CODE 未获取！";
			}
			
			return $userinfo;
		}
		//用于获取用户基础信息
		public function UserBaseMsg($open_id){
			$ACCESS_TOKEN = $this->getAccessToken();
			$UserBaseMsg_url="https://api.weixin.qq.com/cgi-bin/user/info?access_token=$ACCESS_TOKEN&openid=$open_id&lang=zh_CN";
			$userinfo = $this->request->quickGet($UserBaseMsg_url);

			$userinfo=json_decode($userinfo,true);

			return $userinfo;
		}

		//订阅号与服务号权限的Access Token获取,并全局保存
		public function getAccessToken(){
				$data = json_decode($this->request->quickGet("access_token.json"));

				if($data->expire_time < time()){
					$APPID = $this->APPID;
					$APPSECRET = $this->SECRET;
					$url_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$APPID&secret=$APPSECRET";
					$access_token = json_decode($this->request->quickGet($url_token));

					if($access_token->access_token){
						$data->access_token = $access_token->access_token;
						$data->expire_time = time() + 7100;

						$fp = fopen("access_token.json","w");
						fwrite($fp, json_encode($data));
						fclose($fp);
					}
				}

				return $data->access_token;
		}

		//JS-SDK调用的临时凭证
		private function getJsApiTicket(){
			$data = json_decode(($this->request->quickGet("jsapi_ticket.json")));

			if($data->expire_time < time()){
				$AccessToken = $this->getAccessToken();
				
				$jsapi_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$AccessToken";

				$result = json_decode($this->request->quickGet($jsapi_url));
				
				if($result->ticket){
					$data->expire_time = time()+7100;
					$data->jsapi_ticket = $result->ticket;

					$fp = fopen("jsapi_ticket.json","w");
					fwrite($fp,json_encode($data));
					fclose($fp);
				}
			}

			return $data->jsapi_ticket;
		}

		//加密后的JS-SDK凭证
		public function getSignPackage(){
			$jsapi_ticket = $this->getJsApiTicket();
			$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || ($_SERVER['SERVER_PORT'] == 443)?"https://":"http://";

			$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
			$timeStamp = time();

			$nonceStr = \Org\Util\String::randString(16,0,"0123456789");
			
			$string = "jsapi_ticket=$jsapi_ticket&noncestr=$nonceStr&timestamp=$timeStamp&url=$url";

			$signature = sha1($string);
			$signPackage = array(
				'APPId' => $this->APPID,
				'nonceStr' => $nonceStr,
				'timeStamp' => $timeStamp,
				'signature' => $signature
				);

			return $signPackage;
		}

	}
	?>